package com.jieshao

import com.jieshao.rule.ScriptContext
import com.jieshao.script.evalString
import com.jieshao.script.preScript
import com.jieshao.setting.ApplicationSettings
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.DisplayName
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.CsvSource
import org.mockito.kotlin.spy

class RuleTest {

    private var applicationSettings = spy<ApplicationSettings> {

    }


    @ParameterizedTest(name = "''{0}''{index}")
    @DisplayName("testRule")
    @CsvSource(
        "GitHub, https://github.com/my/repo, b032a0707beac9a2f24b1b7d97ee4f7156de182c, /src/Foo.java,1,1, https://github.com/my/repo/blob/b032a0707beac9a2f24b1b7d97ee4f7156de182c/src/Foo.java#L1",
        "GitHub, https://github.com/my/repo, b032a0707beac9a2f24b1b7d97ee4f7156de182c, /src/Foo.java,1,2, https://github.com/my/repo/blob/b032a0707beac9a2f24b1b7d97ee4f7156de182c/src/Foo.java#L1-L2",
        "Azure, https://dev.azure.com/LY1806620741/_git/test, b032a0707beac9a2f24b1b7d97ee4f7156de182c, /src/Foo.java,10,21, https://dev.azure.com/LY1806620741/_git/test?version=GCb032a0707beac9a2f24b1b7d97ee4f7156de182c&path=src%2FFoo.java&line=10&lineEnd=21&lineStartColumn=1&lineEndColumn=1",
        "Azure, https://dev.azure.com/LY1806620741/test, b032a0707beac9a2f24b1b7d97ee4f7156de182c, /src/Foo.java,10,21, https://dev.azure.com/LY1806620741/_git/test?version=GCb032a0707beac9a2f24b1b7d97ee4f7156de182c&path=src%2FFoo.java&line=10&lineEnd=21&lineStartColumn=1&lineEndColumn=1",
//        "Azure, https://dev.azure.com/company/project/_git/test, b032a0707beac9a2f24b1b7d97ee4f7156de182c, src/Foo.java,10,21, https://dev.azure.com/company/project/_git/test/commit/b032a0707beac9a2f24b1b7d97ee4f7156de182c&path=src%2FFoo.java&line=10&lineEnd=21&lineStartColumn=1&lineEndColumn=1",
//        "Azure, https://dev.azure.com/company/project/test, b032a0707beac9a2f24b1b7d97ee4f7156de182c, src/Foo.java,10,21, https://dev.azure.com/company/project/_git/test/commit/b032a0707beac9a2f24b1b7d97ee4f7156de182c&path=src%2FFoo.java&line=10&lineEnd=21&lineStartColumn=1&lineEndColumn=1",
        "BitbucketCloud, https://bitbucket.org/foo/bar, b032a0707beac9a2f24b1b7d97ee4f7156de182c, /src/Foo.java,10,20, https://bitbucket.org/foo/bar/src/b032a0707beac9a2f24b1b7d97ee4f7156de182c/src/Foo.java#lines-10:20",
        "BitbucketServer, https://bitbucket.xxx.net/scm/projectname/repository-name.git, b032a0707beac9a2f24b1b7d97ee4f7156de182c, /src/Foo.java,10,20, https://bitbucket.xxx.net/projects/projectname/repos/repository-name/browse/src/Foo.java?at=b032a0707beac9a2f24b1b7d97ee4f7156de182c#L10-20",
//        "Chromium, https://chromium.googlesource.com/chromiumos/platform/ec, b032a0707beac9a2f24b1b7d97ee4f7156de182c, /src/Foo.java,10,20, https://source.chromium.org/chromiumos/chromiumos/codesearch/+/b032a0707beac9a2f24b1b7d97ee4f7156de182c:src/platform/ec/board/foo.c;l=10-20",
//        "Chromium, https://chromium.googlesource.com/chromium/tools/build, b032a0707beac9a2f24b1b7d97ee4f7156de182c, /src/Foo.java,10,20, https://source.chromium.org/chromium/chromium/tools/build/+/b032a0707beac9a2f24b1b7d97ee4f7156de182c:board/foo.c;l=10-20",
    )
    fun testRule(
        ruleName: String,
        origin: String,
        commit: String,
        filepath: String,
        lineSelectionStart: Int,
        lineSelectionEnd: Int,
        expectedUrl: String
    ) {
        val rule = applicationSettings.findRuleByCustomDomain(origin)!!

        Assertions.assertEquals(ruleName, rule::class.simpleName)
        val scriptContext = ScriptContext()
        scriptContext.origin = origin
        scriptContext.currentRevision = commit
        scriptContext.filePath = filepath
        scriptContext.lineSelection = lineSelectionStart to lineSelectionEnd
        scriptContext.isLocalFile = true

        Assertions.assertEquals(expectedUrl, evalString(rule.template, scriptContext),preScript(scriptContext)+rule.template)
    }
}